<div id="cboxOverlay" style="opacity: 1; cursor: auto; display: none;"></div>
<h1>My Family</h1>
<?php    $str     = '';    ?>

<div class="bold-red pad5 mb12">
    <div class="left w250">Name</div>
    <div class="left w250">Relation</div>
    <div class="left w250">Actions</div>
    <div class="clear"></div>
</div>

<?php
//echo '<br><pre>';
//print_r($this->femaleRelations);
//echo '<br></pre>';

    $clone_family = clone $this->family;
    $key_single_relation = array_keys($this->singleRelations);
    $key_female_relations = array_keys($this->femaleRelations);

    //first adding which relations a user can add.

    $str    .= '<div class="pad5"><div class="left w250"><b>';
    $str    .= $this->userData->FirstName . ' ';
    if(trim($this->userData->MiddleName) != '')
        $str    .= $this->userData->MiddleName . ' ';

    $str    .= $this->userData->LastName . ' ';
    $str    .= '</b></div><div class="left w250">';
    $str    .= 'Your self';
    $str    .= '</div><div class="left">';
    
    $actionstr = '';

    foreach($this->userAllowedRelationAdditions as $relid => $tr)
    {
        if(in_array($relid, $key_female_relations))
            $gender = 0;
        else
            $gender = 1;

        unset($onclick_str);
        $onclick_str = "initRelation('$relid', '', '$gender' )";
        $actionstr .= '<a class="reladd" href="javascript:;" onclick="'.$onclick_str.'">Add ' . $tr . '</a>';
        $actionstr .= '<br>';
            
    }
    $str    .= $actionstr;
    $str    .= '</div><div class="clear"></div></div>';

    foreach($this->family as $rel ):   ?>
    <?php
            $r = explode( '/',  $rel->family_lineage );
           
            $relation = '';
            if( isset($r[0]) )
            {
                 for($i = 1; $i < count($r); $i++ )
                 {
                       $relation .= $this->my_rels[$r[$i]] . '\'s ' ;
                 }
                 $relation .= $rel->RelationName;
            }
            else
            {
                $relation = $rel->RelationName;
            }
  
            $str    .= '<div class="pad5"><div class="left w250">';
            $str    .= $rel->FirstName . ' ';
            if(trim($rel->MiddleName) != '')
                $str    .= $rel->MiddleName . ' ';

            $str    .= $rel->LastName . ' ';
            $str    .= '</div><div class="left w250">';
            $str    .= $relation;
            $str    .= '</div><div class="left">';

            if(isset($actionstr))
                unset($actionstr);
           
            $actionstr = '';
            foreach($this->relationAddArray[$rel->relation_id]['can_add_relations'] as $add_rel)
            {
                $leave_flag = false;
                foreach($clone_family as $cf )
                {
                    if( strlen($cf->family_lineage) > 2 )
                    {
                        $exp = explode( '/' ,$cf->family_lineage);
                        
                        if( in_array($rel->relation_id, $exp))
                        {
                            if( in_array($add_rel['id'], $key_single_relation ) )
                            {
                                $leave_flag = true;
                                break;
                            }
                        }
                    }
                }
                if($leave_flag)
                    continue;
                else
                {
                    unset($onclick_str);
                    if(in_array($add_rel['id'], $key_female_relations))
                        $gender = 0;
                    else
                        $gender = 1;
                    $onclick_str = "initRelation('{$add_rel['id']}', '{$rel->id}', '$gender' )";
                    $actionstr .= '<a class="reladd" href="javascript:;" onclick="'.$onclick_str.'">Add ' .  $add_rel['relation_name'] . '</a>';

                    $actionstr .= '<br>';
                }
            }

            $str    .= $actionstr;
            $str    .= '</div><div class="clear"></div></div>';
            
    ?>

<?php endforeach; ?>

<?php    echo $str;     ?>


<div style="display:none">
    <div id="inline_example1" style="padding:10px; background:#fff;">
        <form method="post" action="/family/index/saverelation" id="relative-form">
            <div class="clearfix column_wrapp">
                    <div class="frm_row" id="hookError" style="color:red; font-weight:bold;height:20px;">
                    </div>
                    <div class="frm_row">
                        <label for="email">First Name *</label>
                        <input type="text" name="firstname" id="firstname" class="frmInput validate[required]">
                    </div>

                    <div class="frm_row">
                        <label for="email">Middle Name *</label>
                        <input type="text" name="middlename" id="middlename" class="frmInput">
                    </div>

                    <div class="frm_row">
                        <label for="email">Last Name *</label>
                        <input type="text" name="lastname" id="lastname" class="frmInput validate[required]">
                    </div>
                   
                    <div class="frm_row">
                        <label for="email">Gender *</label>
                        <input type="text" name="gender" id="gender" readonly="true" value="" class="frmInput">
                    </div>
                    <input type="hidden" name="relation_id" id="relation_id" value="" />
                    <input type="hidden" name="parent_id" id="parent_id" value="" />
                   
                    <div class="frm_row">
                        <input type="submit" value="SAVE" class="submitBtn">
                    </div>
                </div>
        </form>
    </div>
</div>

<script type="text/javascript">
//<![CDATA[

jQuery(document).ready(function(){
    // binds form submission and fields to the validation engine
    jQuery("#relative-form").validationEngine({
					ajaxFormValidation: true,
                    onAjaxFormComplete: ajaxValidationCallback
                });

            $("#relative-form").bind("jqv.form.validating", function(event){
                    $("#hookError").html("")
            })

            $("#relative-form").bind("jqv.form.result", function(event , errorFound){
                    if(errorFound) $("#hookError").append("There are some incorrect values in form");
            })
});

    function ajaxValidationCallback(status, form, json, options){
        if (window.console)
            console.log(status);
        
        if (status === true) {
            window.location.reload();
        }
        else
            alert("invalid form");
    }
    function initRelation(relation_id, parent_id, gender)
    {
        $('#relation_id').val(relation_id);
        $('#parent_id').val(parent_id);
        if(gender == 0)
            $('#gender').val('Female');
        else if(gender == 1)
            $('#gender').val('Male');

        $.colorbox({width:'600px;', inline:true, href:"#inline_example1", onClosed: function(){$(".formErrorContent").hide();  $(".formErrorArrow").hide(); }  });
    }
//]]>
</script>
